﻿package core.dialog {
	
	import base.BaseMc;
	import flash.events.MouseEvent;
	import core.events.CmdEvent;
	import core.net.socket.cmd.BluetoothDiscoveryCmd;
	import core.net.socket.cmd.BluetoothConnectCmd;
	import core.net.socket.cmd.BluetoothBindCmd;
	import core.display.AlphaBg;
	import core.User;
	import core.net.socket.cmd.BluetoothDisConnectCmd;
	import core.events.BlueToothConnectEvent;
	public class DialogBluetoothDiscovery extends BaseMc {
		
		private var address:String;
		private var bg:AlphaBg;
		
		public function DialogBluetoothDiscovery() {
			
		}
		
		protected override function onInit():void {
			super.onInit();
			bg = new AlphaBg();
			this.parent.addChild(bg);
			this.parent.swapChildren(bg,this);
			this.btn_closed.visible = false;
			this.btn_closed.addEventListener(MouseEvent.CLICK,onClosedClick);
			this.btnConnect.visible = false;
			this.btnConnect.addEventListener(MouseEvent.CLICK,onConnectClick);
			this.btnScan.addEventListener(MouseEvent.CLICK,onDiscoveryClick);
			this.btnClose.addEventListener(MouseEvent.CLICK,onCloseClick);
			this.contentText.text = "魔法棒开机后，请点击扫描按钮，开始扫描魔法棒设备";
			this.mfb_logo.visible=false;
			this.scale(1.5,1.5);
			this.x = (this.stage.stageWidth - this.width) / 2;
			this.y = (this.stage.stageHeight - this.height) / 2;
		}
		
		protected override function onRemove():void {
			super.onRemove();
			
			if(bg && this.parent.contains(bg)){
				this.parent.removeChild(bg);
			}
			this.btnConnect.removeEventListener(MouseEvent.CLICK,onConnectClick);
			this.btnScan.removeEventListener(MouseEvent.CLICK,onDiscoveryClick);
			this.btn_closed.removeEventListener(MouseEvent.CLICK,onClosedClick);
			this.btnClose.removeEventListener(MouseEvent.CLICK,onCloseClick);
		}
		
		private function onCloseClick(e:MouseEvent):void {
			onClosedClick(e);
		}
		
		
		//开始连接
		public function onConnectClick(e:MouseEvent):void {
			this.sendCmd(BluetoothConnectCmd.make(this.address));
		}
		
		
		//开始扫描
		public function onDiscoveryClick(e:MouseEvent):void {
			this.btnClose.removeEventListener(MouseEvent.CLICK,onCloseClick);
			this.btnScan.removeEventListener(MouseEvent.CLICK,onDiscoveryClick);
			this.contentText.text = "正在扫描魔法棒……";
			this.sendCmd(BluetoothDiscoveryCmd.make());
			this.btnScan.visible = false;
		}
		//关闭
		public function onClosedClick(e:MouseEvent):void {
			this.parent.removeChild(bg);
			this.sendCmd(BluetoothDisConnectCmd.make());
			this.remove();
			 
		}
		
		private function showConnectView():void {
			//切换按钮
			this.btnConnect.visible = true;
			this.btnScan.visible = false;
			
			//文字信息隐藏
			//this.contentText.visible = false;
			
			//TODO 显示设备
			
		}
		
		private function bindDevice():void{
			this.sendCmd(BluetoothBindCmd.make(this.address,User.id+""));
		}
		 
		
		protected override function onCommandResponse(e:CmdEvent):void {
			super.onCommandResponse(e);
			var data:Object = e.data;
			if(data.cmd == BluetoothDiscoveryCmd.COMMAND_ID){
				this.btnClose.addEventListener(MouseEvent.CLICK,onCloseClick);
				if(data.error == ""){ 
					trace(data.data["address"]);
					address=data.data["address"];
					this.showConnectView();
					this.contentText.visible = true;
					this.contentText.text = "已找到魔法棒 :"+address;
					this.mfb_logo.visible=true;
				}else{
					this.btnScan.visible = true;
					this.contentText.visible = true;
					this.contentText.text = "未扫描到魔法棒设备，请确保魔法棒已开机";
					this.btnScan.addEventListener(MouseEvent.CLICK,onDiscoveryClick);
				}
			}
			else if(data.cmd == BluetoothConnectCmd.COMMAND_ID){
				if(data.error == ""){ 
					this.bindDevice();
				}else{
					
				}
			}else if(data.cmd ==  BluetoothBindCmd.COMMAND_ID){ 
				if(data.data.errcode == 0){
					User.deviceAddress=this.address;
					this.contentText.visible = true; 
					this.contentText.text = "恭喜您，连接魔法棒成功";
					this.btn_closed.visible = true;
					this.btnConnect.visible = false;
					this.dispatchEvent(new BlueToothConnectEvent(BlueToothConnectEvent.BLUETOOTHCONNECT));
				}else
				{
					this.contentText.visible = true;
					this.contentText.text = data.data.message;
				}
			} 
			
		}
	}
	
	
}
